using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Sauce.Internal.Extension;
using Voodoo.Sauce.Internal.Utils;

namespace Voodoo.Sauce.Debugger
{
	public class EventConsoleListScreen : Screen
	{
		private const string DEFAULT_WRAPPER_NAME_FILTER = "VoodooAnalytics";

		[Header("EventConsole")]
		[SerializeField]
		private EventConsoleInformationScreen eventInformationScreen;

		[SerializeField]
		private EventConsoleFiltersScreen eventFiltersScreen;

		[SerializeField]
		private Transform sessionContainer;

		[SerializeField]
		private Button recordButton;

		[SerializeField]
		private Button flushButton;

		[SerializeField]
		private Button filterButton;

		[SerializeField]
		private Image recordingImage;

		[SerializeField]
		private Image pauseImage;

		[SerializeField]
		private Button recordAtStartupBtnOn;

		[SerializeField]
		private Button recordAtStartupBtnOff;

		[Header("Prefab")]
		[SerializeField]
		private EventConsoleItem eventLogItemPrefab;

		[SerializeField]
		private EventConsoleSessionItem sessionItemPrefab;

		private static readonly ConcurrentQueue<Action> _runOnMainThread = new ConcurrentQueue<Action>();

		private readonly Dictionary<string, EventConsoleItem> _eventDictionary = new Dictionary<string, EventConsoleItem>();

		private readonly Dictionary<string, EventConsoleSessionItem> _sessionDictionary = new Dictionary<string, EventConsoleSessionItem>();

		private readonly Stack<EventConsoleItem> _eventPool = new Stack<EventConsoleItem>();

		private void Awake()
		{
			recordButton.onClick.AddListener(RecordPressed);
			flushButton.onClick.AddListener(FlushLog);
			filterButton.onClick.AddListener(ShowFiltersScreen);
			recordAtStartupBtnOn.onClick.AddListener(OnRecordAtStartupBtnClick);
			recordAtStartupBtnOff.onClick.AddListener(OnRecordAtStartupBtnClick);
			eventFiltersScreen.EventConsoleListScreen = this;
		}

		private void Start()
		{
			RefreshEventLogToScreen();

		}

		private void OnDestroy()
		{

			recordButton.onClick.RemoveListener(RecordPressed);
		}

		private void Update()
		{
			if (!_runOnMainThread.IsEmpty)
			{
				Action result;
				while (_runOnMainThread.TryDequeue(out result))
				{
					result?.Invoke();
				}
			}
		}

		private void OnEnable()
		{
			StartCoroutine(RefreshOnEnable());
			UpdateRecordAtStartupBtnDisplay();
			UpdateRecordButtonState();
		}

		private IEnumerator RefreshOnEnable()
		{
			yield return null;
			RefreshSessionsSize();
		}

		private static void EnableAnalyticsLoggingAndDebugging(bool isEnabled)
		{

		}

		private static void UpdateButtonState(bool enable, Selectable button, Graphic buttonIcon)
		{
			if (button.interactable != enable)
			{
				button.interactable = enable;
				ColorBlock colors = button.colors;
				buttonIcon.color = (enable ? colors.normalColor : colors.disabledColor);
			}
		}

		private void ShowFiltersScreen()
		{
			Debugger.Show(eventFiltersScreen);
		}

		internal void RefreshEventLogToScreen()
		{


		}


		private void RefreshSessionsSize()
		{
			foreach (KeyValuePair<string, EventConsoleSessionItem> item in _sessionDictionary.ToList())
			{
				item.Value.GetContainer.RefreshHierarchySize();
			}
		}



		private EventConsoleSessionItem CreateSession(string sessionId)
		{
			EventConsoleSessionItem eventConsoleSessionItem = UnityEngine.Object.Instantiate(sessionItemPrefab, sessionContainer);
			eventConsoleSessionItem.transform.SetAsFirstSibling();
			_sessionDictionary.Add(sessionId, eventConsoleSessionItem);
			eventConsoleSessionItem.Initialize(_sessionDictionary.Count, sessionId);
			return eventConsoleSessionItem;
		}

		private void FlushLog()
		{

			RefreshEventLogToScreen();
			RefreshSessionsSize();
		}

		private void RecordPressed()
		{

			UpdateRecordButtonState();
		}

		private void OnRecordAtStartupBtnClick()
		{

			UpdateRecordAtStartupBtnDisplay();
		}

		private void UpdateRecordAtStartupBtnDisplay()
		{

		}

		private void UpdateRecordButtonState()
		{

		}
	}
}
